package com.zhucan.account.infrastructure.storage;

import com.zhucan.account.domain.aggregate.Account;
import com.zhucan.account.infrastructure.db.entity.AccountPO;
import com.zhucan.account.domain.repository.AccountStorage;
import com.zhucan.account.infrastructure.db.AccountRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.persistence.EntityNotFoundException;

/**
 * author: zhuCan
 * date: 2020/8/4 19:56
 * description:
 */
@Component
@AllArgsConstructor
public class AccountStorageImpl implements AccountStorage {

  private final AccountRepository accountRepository;

  @Override
  public Account load(Long id) {
    AccountPO accountPO = accountRepository.findById(id)
        .orElseThrow(() -> new EntityNotFoundException("账户不存在"));

    return new Account(accountPO);
  }

  @Override
  public boolean save(Account account) {
    AccountPO po = new AccountPO();
    BeanUtils.copyProperties(account, po);
    accountRepository.save(po);

    return true;
  }
}
